package com.jiang5sx.partnermatching.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiang5sx.partnermatching.common.AjaxResult;
import com.jiang5sx.partnermatching.constant.HttpStatus;
import com.jiang5sx.partnermatching.constant.UserConstant;
import com.jiang5sx.partnermatching.domain.User;
import com.jiang5sx.partnermatching.domain.dto.UserDto;
import com.jiang5sx.partnermatching.domain.vo.UserVo;
import com.jiang5sx.partnermatching.exception.ServiceException;
import com.jiang5sx.partnermatching.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Api(value = "用户中心Controller")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "查询用户列表")
    @GetMapping("/list")
    public AjaxResult list(User user, HttpServletRequest request) {
        if (this.verifyIdentity(request)){
            throw new ServiceException("当前用户没有权限", HttpStatus.NO_AUTH);
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(user.getUsername()), User::getUsername, user.getUsername());
        List<User> data = userService.list(wrapper);
        List<UserVo> userVos = new ArrayList<>();
        for (User user1 :data) {
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user1,userVo);
            userVos.add(userVo);
        }

        return AjaxResult.success(userVos);
    }

    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody UserDto userDto) {
        String account = userDto.getAccount();
        String password = userDto.getPassword();
        String checkPassword = userDto.getCheckPassword();
        String planetCode = userDto.getPlanetCode();
        if (StringUtils.isAnyBlank(account,planetCode,password,checkPassword)){
            throw new ServiceException("用户账号或密码或星球编号为空",HttpStatus.WARN);
        }
        return AjaxResult.success(userService.register(account,password,checkPassword,planetCode));
    }


    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody UserDto userDto, HttpServletRequest request) {
        return AjaxResult.success(userService.login(userDto.getAccount(), userDto.getPassword(), request));
    }

    @ApiOperation(value = "根据用户id删除用户信息")
    @DeleteMapping("/delete/{userId}")
    public AjaxResult deleteById(@PathVariable Long userId, HttpServletRequest request) {
        if (this.verifyIdentity(request)){
            throw new ServiceException("当前用户没有权限", HttpStatus.NO_AUTH);
        }
        return AjaxResult.success(userService.removeById(userId));
    }


    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public void logout(HttpServletRequest request){
        request.getSession().removeAttribute(UserConstant.USER_STATUS);
    }

    @ApiOperation("获取当前用户信息")
    @GetMapping("/current")
    public AjaxResult getCurrentUser(HttpServletRequest request){
        User currentUser = (User)request.getSession().getAttribute(UserConstant.USER_STATUS);
        if (currentUser == null){
            throw new ServiceException("当前用户不存在",HttpStatus.ERROR);
        }
        User user = userService.getById(currentUser.getId());
        if (user == null){
            throw new ServiceException("当前用户不存在",HttpStatus.ERROR);
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);
        return AjaxResult.success(userVo);
    }

    @ApiOperation("根据标签查询用户信息")
    @GetMapping("/tags")
    public AjaxResult findAllByTags(@RequestParam("tags") List<String> tags){
        return AjaxResult.success(userService.findAllByTags(tags));
    }

    /**
     * 身份校验
     *
     * @param request
     * @return
     */
    private boolean verifyIdentity(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute(UserConstant.USER_STATUS);
        return user == null || user.getRole() != UserConstant.ADMIN_ROLE;
    }
}
